var Cw = function() {
	var tableColum = [
			{
				checkbox : true,
			},
			{
				title : '仓房名称',
				field : 'housename',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '仓房类型',
				field : 'typename',
				align : 'center',
				valign : 'middle'
			},
			{
				title : '储粮性质',
				field : 'grainpropertyname',
				align : 'center',
				valign : 'middle'
			},
			{
				title : '测温结果',
				field : 'status',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '检测时间',
				field : 'checktime',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '最高粮温',
				field : 'highttemp',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '最低粮温',
				field : 'lowertemp',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '平均粮温',
				field : 'avgtemp',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '仓内温度',
				field : 'intemp',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '仓内湿度',
				field : 'inhumidity',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '仓外温度',
				field : 'outtemp',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '仓外湿度',
				field : 'outhumidity',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '状态',
				field : 'status',
				align : 'center',
				valign : 'middle',
			},
			{
				title : '操作',
				field : 'cz',
				align : 'center',
				valign : 'middle',
				formatter : function(value, row, index) {
					return '<a href="javascript:Cw.updateRow(\'' + index + '\',\'' + row.storehousecode + '\',\''
							+ row.id + '\');" class="btn btn-success">更新</a><a href="javascript:Cw.openDetail(\''
							+ row.storehousecode
							+ '\');" class="btn btn-success">详情</a><a href="javascript:Cw.openTimeDetail(\''
							+ row.storehousecode + '\');" class="btn btn-success">实时</a>';
				}
			} ];

	var initPingpu = function() {
		$
				.ajax({
					type : "GET",
					cache : false,
					url : "lq/GLlist?orgcode=" + orgcode,
					success : function(responseJson) {
						if (responseJson.success == true) { // 返回true
							var data = responseJson.obj;
							if (data.length > 0) {
								$("#pingpuLQView").empty();
								$
										.each(
												data,
												function(n, value) {
													var div = "<div class=\"col-lg-3 col-md-3 col-sm-6 col-xs-12 default-view"
															+ value.housename + "\">";
													div = div
															+ "<div class=\"dashboard-stat dashboard-stat-v2 default\">";
													div = div
															+ "<div class=\"visual\"><img src=\"assets/global/img/picture/small.png\" /><div>&nbsp;&nbsp;"
															+ value.housename + "</div></div>";
													div = div + "<div class=\"details\"><div class=\"number\"></div>";
													div = div + "<table class=\"desc\">";
													div = div + "<tr><td>储粮类型：</td><td>" + value.grainpropertyname
															+ "</td></tr>";
													div = div + "<tr><td>外温：</td><td>" + value.outtemp + "</td></tr>";
													div = div + "<tr><td>仓温：</td><td>" + value.intemp + "℃</td></tr>";
													div = div + "<tr><td>粮温：</td><td>" + value.avgtemp + "℃</td></tr>";
													div = div + "<tr><td>当前状态：</td><td>" + value.status + "</td></tr>";
													div = div + "</table>";
													div = div + "</div>";
													div = div
															+ "<div style=\"margin-top: 160px; text-align: center; margin-bottom: 10px; font-weight: 500;clear:both;\">检测时间："
															+ value.checktime + "</div></div></div>";
													$("#pingpuLQView").append($(div));
													$(".default-view" + value.housename).on("click", function() {
														openDetail(value.storehousecode);
													});
												});
							}
						} else {
							QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
						}
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
					}
				});
	}

	var openDetail = function(id) {
		QTYYComponents.layerOpen("数据详情", '1200px', '680px', "lq/sjxq/main?id=" + id);
	}

	var openTimeDetail = function(storehousecode) {
		QTYYComponents.layerOpen("实时数据", '1200px', '680px', "lq/ss/main?storehousecode=" + storehousecode);
	}

	var updateRow = function(index, storehousecode, recordId) {
		App.blockUI({
			overlayColor : 'blue',
			message : '正在更新中，请稍后'
		});
		$.ajax({
			type : "GET",
			cache : false,
			url : "startstop/lqsave?orgcode=" + orgcode + "&storehousecode=" + storehousecode,
			success : function(responseJson) {
				if (responseJson.success == true) {
					var i = 0;
					var dingshizhixing = setInterval(function() {
						// 需要轮询10次，间隔1秒
						$.ajax({
							type : "GET",
							cache : false,
							url : "lq/GLlist?orgcode=" + orgcode + "&storehousecode=" + storehousecode,
							success : function(responseJson) {
								if (responseJson.success == true) { // 返回true
									$("#cwTable").bootstrapTable('updateRow', {
										index : index,
										row : responseJson.obj[0]
									});
									if (responseJson.obj[0].id != recordId) {
										window.clearInterval(dingshizhixing);
										App.unblockUI();
										QTYYComponents.bootstrapSweetAlert("", "更新成功", "info");
									}
								} else {
									QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
								}
							},
							error : function(XMLHttpRequest, textStatus, errorThrown) {
								QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
							}
						});
						i++;
						if (i == 10) {
							window.clearInterval(dingshizhixing);
							App.unblockUI();
							QTYYComponents.bootstrapSweetAlert("", "未查询到更新结果", "info");
						}
					}, 1000);
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			}
		});
	}

	var updateBatchRow = function() {
		var rows = $("#cwTable").bootstrapTable('getSelections');
		if(rows.length < 1){
			QTYYComponents.bootstrapSweetAlert("", "请先选择批量更新的记录", "info");
			return;
		}
		App.blockUI({
			overlayColor : 'blue',
			message : '正在更新中，请稍后'
		});
		var storehousecode = "";
		for (var i = 0; i < rows.length; i++) {
			if (rows[i][0] == true) {
				storehousecode = storehousecode + rows[i].storehousecode + ",";
			}
		}
		$.ajax({
			type : "GET",
			cache : false,
			url : "startstop/lqsave?orgcode=" + orgcode + "&storehousecode="
					+ storehousecode.substring(0, storehousecode.length - 1),
			success : function(responseJson) {
				if (responseJson.success == true) {
					var i = 0;
					var dingshizhixing = setInterval(function() {
						// 需要轮询10次，间隔1秒
						$.ajax({
							type : "GET",
							cache : false,
							url : "lq/GLlist?orgcode=" + orgcode,
							success : function(responseJson) {
								if (responseJson.success == true) { // 返回true
									for (var i = 0; i < rows.length; i++) {
										for (var j = 0; j < responseJson.obj.length; j++) {
											if (rows[i][0] == true) {
												if (rows[i].storehousecode == responseJson.obj[j].storehousecode) {
													if (rows[i].id != responseJson.obj[j].id) {
														$("#cwTable").bootstrapTable('updateRow', {
															index : i,
															row : responseJson.obj[j]
														});
														window.clearInterval(dingshizhixing);
														App.unblockUI();
														QTYYComponents.bootstrapSweetAlert("", "更新成功", "info");
													}
												}
											}
										}
									}
								} else {
									QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
								}
							},
							error : function(XMLHttpRequest, textStatus, errorThrown) {
								QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
							}
						});
						i++;
						if (i == 10) {
							window.clearInterval(dingshizhixing);
							App.unblockUI();
							QTYYComponents.bootstrapSweetAlert("", "未查询到更新结果", "info");
						}
					}, 1000);
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			}
		});
	}

	var initLiebiao = function() {
		QTYYComponentsTable.initBootStrapTable($("#cwTable"), "/lq/LBlist", tableColum);
	}

	/**
	 * 绑定按钮事件
	 */
	var eventHandler = function() {
		$("#liebiaoBtn").on("click", function() {
			$("#pingpuLQView").css("display", "none");
			$("#liebiaoLQView").css("display", "block");
		});
		$("#pingpuBtn").on("click", function() {
			$("#pingpuLQView").css("display", "block");
			$("#liebiaoLQView").css("display", "none");
		});
		$("#searchBtn").on("click", function() {
			QTYYComponentsTable.searchTable("cwTable", "searchForm");
		});
		$("#qbsxBtn").on("click", function() {
			initPingpu();
			QTYYComponentsTable.searchTable("cwTable", "searchForm");
		});
		$("#plgxBtn").on("click", function() {
			updateBatchRow();
		});

	}

	return {
		init : function() {
			QTYYComponents.initSelect("/dict/cangfang", "storehousecode");
			QTYYComponents.initSelect("", "status");
			QTYYComponents.initDateRange("jcsj");
			eventHandler();
			initPingpu();
			initLiebiao();
			// g_intervalId = window.setInterval(function() {
			// initPingpu();
			// rrefreshTable();
			// }, 5000);
		},
		openDetail : function(id) {
			openDetail(id);
		},
		updateRow : function(index, storehousecode, recordId) {
			updateRow(index, storehousecode, recordId);
		},
		updateBatchRow : function() {
			updateBatchRow();
		},
		openTimeDetail : function(storehousecode) {
			openTimeDetail(storehousecode);
		}
	}
}();

function rrefreshTable() {
	QTYYComponentsTable.searchTable("cwTable", "");
}

jQuery(document).ready(function() {
	Cw.init();
});